<?php
namespace app\admin\controller;

/**
 * 学生课程进度类
 * 
 * 
 * @author Aninet Sun Jun 17 13:39:15 2018
 */
use app\admin\model\Process;
use \think\Validate;
use \think\Config;

use \app\admin\model\StudentCourse as StudentCourse;
use \app\admin\model\Student as Student;
    
class Course extends Base{

    /**
     * 显示所有学生的进度，有分页。
     * 
     * @return index.html
     * @date Sun Jun 17 13:39:34 2018
     */
    public function index(){
        $get = $this->req->get();
        $validate = new Validate();
        $model = new Student;
        $data = $model->field('stu_id,stu_name')->where(['stu_state'=> 0])->paginate(Config::get('paginate.list_rows'),true);
        $this->assign('data',$data);
        return $this->fetch();
    }

    /**
     * 学生个人学习进度查看
     * 
     * @return html
     * @date Fri Jul 20 12:38:18 2018
     */
    // public function checkone(){
    //     $get = $this->req->get();
    //     $rule = [
    //         'id' => 'require|number'
    //     ];
    //     $validate = new Validate($rule);
    //     if($validate->check($get)){
    //         $model = new \app\admin\model\StudentCourse;
    //         $course = $model->where(['stu_id'=>$get['id']])->select();
    //         if($course){
    //             $this->assign('course',$course);
    //             return $this->fetch();
    //         }else{
    //             return $this->error('数据出错');
    //         }
    //     }else{
    //         return $this->error('数据出错');
    //     }
    // }

    /**
     * 查看某个学生的进度
     * 
     * @return 
     * @date Fri Jul 20 12:43:01 2018
     */
    public function course(){
        $post = $this->req->post();
        $rule = [
            'id' => 'require|number'
        ];
        $validate = new Validate($rule);
        if($validate->check($post)){
            $model = new \app\admin\model\StudentCourse;
            $course = $model->where(['stu_id'=>$post['id']])->select();
            if($course){
                $info['msg'] = '所有课程';
                $this->assign('course',$course);
                $info['data'] = $this->fetch();
                echo $this->right($info);
            }else{
                $info['msg'] = "没有数据";
                echo $this->wrong($info);
            }
        }else{
            $info['msg'] = '系统错误';
            echo $this->wrong($info);
        }
        return;
    }

    /**
     * 显示学习课程表
     * 
     * @return 
     * @date Sat Jul 21 22:42:49 2018
     */
    public function ls(){
        $post = $this->req->get();
        $rule = [
            'id' => 'require|number'
        ];
        $validate = new Validate($rule);
        if($validate->check($post)){
            $model = new \app\admin\model\StudentCourse;
            $course = $model->where(['stu_id'=>$post['id']])->select();
            if($course){
                $this->assign('course',$course);
                return $this->fetch();
            }else{
                return $this->err('读取数据出错');
            }
        }else{
            return $this->err('找不到数据');
        }
        // echo "<script>window.board.modal('show')</script>";
    }

    
    /**
     * 给多个学生添加创建课程
     * 
     * @return 
     * @date Sun Jun 17 14:41:19 2018
     */
    public function create()
    {
        $stuid = $this->req->get('id');
        if($stuid){
            $model = new \app\admin\model\Student;
            $student = $model->field('stu_id,stu_name')->where('stu_id','in',$stuid)->select();
            if($student){
                $this->assign('student',$student);
            }else{
                return $this->error('系统错误');
            }
            // var_dump($model->getLastSql());
            // var_dump($student);
        }else{
            $model = new \app\admin\model\Student;
            $student = $model->field('stu_id,stu_name')->where(['stu_state'=>0])->select();
            $this->assign('student',$student);
        }
        $model = new \app\admin\model\Book;
        $book = $model->select();
        $this->assign('book',$book);
        return $this->fetch();
    }

    /**
     * 给单个学生添加课程
     * 
     * @return 
     * @date Thu Jul 19 08:41:54 2018
     */
    public function add(){
        $stuid = $this->req->get('id');
        $model = new \app\admin\model\Student;
        $student = $model->field('stu_id,stu_name')->where(['stu_state'=>0])->find();
        $this->assign('student',$student);
        $model = new \app\admin\model\Book;
        $book = $model->select();
        $this->assign('book',$book);
        return $this->fetch();
    }

    /**
     * 添加新保存课程  
     * 
     * @return 
     * @date Sun Jun 17 15:29:48 2018
     */
    public function insert()
    {
        $post = $this->req->post();

        if(isset($post['stuid'])){
            $rule = [
                'title' => 'require',
                'content' => 'require'
            ];
            $validate = new Validate($rule);
            if($validate->check($post)){
                $content = $post['content'];
                $exercise = $post['exercise'];
                $data['cos_title'] = $post['title'];
                $data['cos_begin'] = $post['begin'];
                $data['cos_close'] = $post['close'];
                $data['cos_content'] = $content;
                $data['cos_sort'] = $post['sort'];
                $data['cos_exercise'] = $exercise;
                $data['cos_mark'] = isset($post['mark']) ? $post['mark'] : '';
                $data['cos_createtime'] = time();
                $model = new \app\admin\model\StudentCourse;
                $result = $model->batchInsert($post['stuid'],$data);
                if($result){
                    $info = [
                        'msg' => "添加失败"
                    ];
                    echo $this->wrong($info);
                }else{
                    $info = [
                        'msg' => "添加成功"
                    ];
                    echo $this->right($info);            

                }
            }else{
                $info['msg'] = "提交的数据有漏";
                echo $this->wrong($info);                
                
            }
        }else{
            $info['msg'] = "你没有选择学生";
            echo $this->wrong($info);
            
        }
        return;
    }

    /**
     * 添加新保存课程  
     * 
     * @return 
     * @date Sun Jun 17 15:29:48 2018
     */
    public function save()
    {
        $post = $this->req->post();

        if(isset($post['stuid'])){
            $rule = [
                'title' => 'require',
                'content' => 'require'
            ];
            $validate = new Validate($rule);
            if($validate->check($post)){
                $data['stu_id'] = $post['stuid'];
                $content = $post['content'];
                $exercise = $post['exercise'];
                $data['cos_title'] = $post['title'];
                $data['cos_begin'] = $post['begin'];
                $data['cos_close'] = $post['close'];
                $data['cos_content'] = $content;
                $data['cos_sort'] = $post['sort'];
                $data['cos_exercise'] = $exercise;
                $data['cos_mark'] = isset($post['mark']) ? $post['mark'] : '';
                $data['cos_createtime'] = time();
                $model = new \app\admin\model\StudentCourse;
                // $result = $model->save($data);
                $result = 1;
                if($result){
                    $info = [
                        'msg' => "添加成功",
                        'state' => 1
                    ];
                }else{
                    $info = [
                        'msg' => "添加失败",
                        'state' => 1
                    ];
                }
            }else{
                $info['msg'] = "提交的数据有漏";
                $info['state'] = 1;
            }
        }else{
            $info['msg'] = "你没有选择学生";
            $info['state'] = 1;
        }
        echo $this->send($info);
        return;
    }

    public function chapter(){
        $post = $this->req->post();
        $rule = [
            'id' => 'require|number'
        ];
        $validate = new Validate($rule);
        if($validate->check($post)){
            $model = new \app\admin\model\BookChapter;
            $chapter = $model->where(['bk_id'=>$post['id']])->select();
            if($chapter){
                $info['msg'] = '所有章节';
                $info['data'] = $chapter;
                echo $this->right($info);
            }else{
                $info['msg'] = "没有数据";
                echo $this->wrong($info);
            }
        }else{
            $info['msg'] = '系统错误';
            echo $this->wrong($info);
        }
        return;
    }

    public function edit(){
        $get = $this->req->get();
        $validate = new Validate(['id'=>'require|number']);
        if($validate->check($get)){
            $cos_model = new \app\admin\model\StudentCourse;
            $cos_data = $cos_model->where(['cos_id'=>$get['id']])->find();
            $stu_model = new \app\admin\model\Student;
            $stu_data = $stu_model->field('stu_id,stu_name')->select();
            foreach($stu_data as $s){
                if($s['stu_id'] == $cos_data['stu_id']){
                    $s['select'] = 'selected';
                } else {
                    $s['select'] = '';
                }
            }
            $model = new \app\admin\model\Book;
            $book = $model->select();
            $this->assign('book',$book);
            $this->assign('stu_data',$stu_data);
            $this->assign('cos_data',$cos_data);
            return $this->fetch();
        }else{
            // return $this->error();
        }
            
    }
    public function update(){
        $post = $this->req->post();
        $rule = [
            'id'      => 'require',
            'stuid'   => 'require',
            'title'   => 'require',
            'content' => 'require',
            'begin'   => 'require',
            'close'   => 'require',
        ];
        $validate = new Validate($rule);
        if($validate->check($post)){
            $content = trim($post['content']);
            $temp = preg_split('/[\r\n]/',$content);
            $main = [];
            foreach($temp as $v){
                $con = explode('/',$v);
                $main[$con[0]] = $con[1];
            }

            $data['stu_id']         = $post['stuid'];
            $data['cos_title']      = $post['title'];
            $data['cos_begin']      = $post['begin'];
            $data['cos_close']      = $post['close'];
            $data['cos_content']    = serialize($main);
            $data['cos_sort']       = $post['sort'];
            $data['cos_exercise']   = $post['exercise'];
            $data['cos_mark']       = isset($post['mark']) ? $post['mark'] : '';
            $data['cos_createtime'] = time();
            $model = new \app\admin\model\StudentCourse;
            $result = $model->where(['cos_id'=>$post['id']])->update($data);
            if($result){
                $info = [
                    'msg' => "修改成功"
                ];
                echo $this->right($info);            
            }else{
                  $info = [
                    'msg' => "修改失败"
                ];
                echo $this->wrong($info);
            }
            
        }else{
            $info['msg'] = "提交的数据有漏";
            echo $this->wrong($info);
                    }
        return;
        
    }

    public function remove(){
        $post = $this->req->post();
        // var_dump($post);exit();
        $rule = [
            'id' => 'require|number'
        ];
        $validate = new Validate($rule);
        if($validate->check($post)){
            $model = new StudentCourse;
            $student = $model->destroy(['cos_id'=>$post['id']]);
            // $student = 1;
            if($student){
                $info['msg'] = '删除成功';
                echo $this->right($info);
            }else{
                $info['msg'] = "删除失败";
                echo $this->wrong($info);
            }
        }else{
            $info['msg'] = '系统错误';
            echo $this->wrong($info);
        }
        return;
    }

    public function state(){
        $post = $this->req->post();
        // var_dump($post);exit();
        $rule = [
            'id' => 'require|number',
            'state' => 'require|number'
        ];
        $validate = new Validate($rule);
        if($validate->check($post)){
            $model = new StudentCourse;
            $result = $model->where(['cos_id'=>$post['id']])->update(['cos_state'=>$post['state']]);
            // $student = 1;
            if($result){
                $info['msg'] = "修改成功";
                echo $this->wrong($info);
            }else{
                $info['msg'] = '修改失败';
                echo $this->right($info);
            }
        }else{
            $info['msg'] = '系统错误';
            echo $this->wrong($info);
        }
        return;
    }        
}
